package com.jhd.fuzhou.view.random;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.vo.DetectedItemsVo;
import com.jhd.fuzhou.vo.GlobalDictionaryVo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jhd.fuzhou.view.login.MainActivity.supervisionInspectionVo;

public class DoubleQuickCheckActivity extends BaseActivity {

    private String checkValue;
    private List<GlobalDictionaryVo> globalDictionaryVoList;
    private AlertDialog _alertDialog=null;
    private Context context= this;
    @BindView(R.id.rv_quick_check)
    RecyclerView rv_quick_check;
    @BindView(R.id.btn_add_quick_check)
    Button btn_add_quick_check;
    private List<DetectedItemsVo> detectedItemsVoList0=new ArrayList<>();//筛选的快速检测
    private List<DetectedItemsVo> detectedItemsVoList=new ArrayList<>();//已经添加过的检测
    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_quick_check);
    }

    @Override
    protected void processLogic() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        show();
    }
    @OnClick({R.id.btn_add_quick_check})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_add_quick_check:
                Intent intent=new Intent(context, DoubleQuickCheckAddActivity.class);
                intent.putExtra("checkValue",checkValue);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        show();
    }

    private void show(){
        if(supervisionInspectionVo.getDetectedItemsVoList()!=null){
            detectedItemsVoList= supervisionInspectionVo.getDetectedItemsVoList();
        }
        checkValue=getIntent().getStringExtra("checkValue");
        LinearLayoutManager layoutManage = new LinearLayoutManager(context);
        rv_quick_check.setLayoutManager(layoutManage);
        detectedItemsVoList0.clear();
        for (DetectedItemsVo detectedItemsVo : detectedItemsVoList) {
            if(detectedItemsVo.getCheckType().equals("0")){
                detectedItemsVoList0.add(detectedItemsVo);
            }
        }
        CheckAdapter adapter=new CheckAdapter(detectedItemsVoList0);
        rv_quick_check.setAdapter(adapter);
    }

    public class CheckAdapter extends RecyclerView.Adapter<CheckAdapter.ViewHolder> {
        private List<DetectedItemsVo> detectedItemsVos;

        public CheckAdapter(List<DetectedItemsVo> detectedItemsVos) {
            this.detectedItemsVos = detectedItemsVos;
        }

        @NonNull
        @Override
        public CheckAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_quick_check, parent, false);
            CheckAdapter.ViewHolder viewHolder = new CheckAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull CheckAdapter.ViewHolder holder, int position) {
            holder.tv_check_number.setText("序号("+(position+1)+")");
            holder.tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DetectedItemsVo detectedItemsVo=detectedItemsVos.get(position);
                    detectedItemsVos.remove(position);
                    detectedItemsVoList.remove(detectedItemsVo);
                    notifyDataSetChanged();
                }
            });
            holder.tv_check_content.setText(detectedItemsVos.get(position).getItemName());
            holder.tv_sample_number.setText(String.valueOf(detectedItemsVos.get(position).getSampleQuantity()));
            holder.tv_qualified_number.setText(String.valueOf(detectedItemsVos.get(position).getQualifiedQuantity()));


        }

        @Override
        public int getItemCount() {
            return detectedItemsVos.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_check_number,tv_delete,tv_check_content,tv_sample_number,tv_qualified_number;
            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_check_number = itemView.findViewById(R.id.tv_check_number);
                this.tv_delete = itemView.findViewById(R.id.tv_delete);
                this.tv_check_content = itemView.findViewById(R.id.tv_check_content);
                this.tv_sample_number = itemView.findViewById(R.id.tv_sample_number);
                this.tv_qualified_number = itemView.findViewById(R.id.tv_qualified_number);
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}